package com.rxyb.wechat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rxyb.core.constants.DbConstants;
import com.rxyb.core.respone.R;
import com.rxyb.core.respone.enums.CommonCode;
import com.rxyb.toolize.date.DateTime;
import com.rxyb.toolize.date.enums.FormatDate;
import com.rxyb.wechat.db.dao.ThirdWechatCfgMapper;
import com.rxyb.wechat.db.po.ThirdWechatCfg;
import com.rxyb.wechat.gateway.dto.cfg.*;
import com.rxyb.wechat.gateway.enums.RspCode;
import com.rxyb.wechat.service.ThirdWechatCfgService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 商户信息 服务实现类
 * </p>
 *
 * @author DUYH
 * @since 2020-06-15
 */
@Service
public class ThirdWechatCfgServiceImpl extends ServiceImpl<ThirdWechatCfgMapper, ThirdWechatCfg> implements ThirdWechatCfgService {


    @Autowired(required = false)
    private ThirdWechatCfgMapper thirdWechatCfgMapper;

    @Override
    public R<CreateWechatCfgRsp> create(CreateWechatCfgReq createWechatCfgReq) {
        final ThirdWechatCfg record = query(createWechatCfgReq.getAppId());
        if (null != record) {
            return R.failed(RspCode.APPID_EXIST);
        }
        ThirdWechatCfg entity = new ThirdWechatCfg();
        BeanUtils.copyProperties(createWechatCfgReq, entity);
        Long time = DateTime.getDateTime();
        entity.setAddTime(time);
        entity.setUpdateTime(time);
        entity.setRowState(DbConstants.ROW_STATE_VALID);
        final int insert = thirdWechatCfgMapper.insert(entity);
        if (insert == 1) {
            CreateWechatCfgRsp createWechatCfgRsp = new CreateWechatCfgRsp();
            createWechatCfgRsp.setUpdateTime(DateTime.getDateFormat(time, FormatDate.FORMAT_TIME_STR));
            return R.ok(createWechatCfgRsp);
        }
        return R.failed(CommonCode.FAILED);
    }

    @Override
    public R<DeleteWechatCfgRsp> delete(DeleteWechatCfgReq deleteWechatCfgReq) {
        Long time = DateTime.getDateTime();
        QueryWrapper<ThirdWechatCfg> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ThirdWechatCfg::getAppId, deleteWechatCfgReq.getAppId());
        wrapper.lambda().eq(ThirdWechatCfg::getRowState, DbConstants.ROW_STATE_VALID);
        ThirdWechatCfg entity = new ThirdWechatCfg();
        entity.setRowState(DbConstants.ROW_STATE_INVALID);
        entity.setDeleteTime(time);
        final int update = thirdWechatCfgMapper.update(entity, wrapper);
        if (1 == update) {
            DeleteWechatCfgRsp deleteWechatCfgRsp = new DeleteWechatCfgRsp();
            deleteWechatCfgRsp.setUpdateTime(DateTime.getDateFormat(time, FormatDate.FORMAT_TIME_STR));
            return R.ok(deleteWechatCfgRsp);
        }
        return R.failed(CommonCode.FAILED);
    }

    @Override
    public R<UpdateWechatCfgRsp> update(UpdateWechatCfgReq updateWechatCfgReq) {
        Long time = DateTime.getDateTime();
        QueryWrapper<ThirdWechatCfg> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ThirdWechatCfg::getAppId, updateWechatCfgReq.getAppId());
        wrapper.lambda().eq(ThirdWechatCfg::getRowState, DbConstants.ROW_STATE_VALID);
        ThirdWechatCfg entity = new ThirdWechatCfg();
        BeanUtils.copyProperties(updateWechatCfgReq, entity);
        entity.setUpdateTime(time);
        final int update = thirdWechatCfgMapper.update(entity, wrapper);
        if (1 == update) {
            UpdateWechatCfgRsp updateWechatCfgRsp = new UpdateWechatCfgRsp();
            updateWechatCfgRsp.setUpdateTime(DateTime.getDateFormat(time, FormatDate.FORMAT_TIME_STR));
            return R.ok(updateWechatCfgRsp);
        }
        return R.failed(CommonCode.FAILED);
    }

    @Override
    public R<GetWechatCfgInfoRsp> query(GetWechatCfgInfoReq getWechatCfgInfoReq) {
        final ThirdWechatCfg record = query(getWechatCfgInfoReq.getAppId());
        if (null == record) {
            return R.failed(CommonCode.FAILED);
        }
        GetWechatCfgInfoRsp getWechatCfgInfoRsp = new GetWechatCfgInfoRsp();
        BeanUtils.copyProperties(record, getWechatCfgInfoRsp);
        return R.ok(getWechatCfgInfoRsp);
    }


    @Cacheable
    @Override
    public ThirdWechatCfg query(String appId) {
        QueryWrapper<ThirdWechatCfg> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ThirdWechatCfg::getAppId, appId);
        return thirdWechatCfgMapper.selectOne(wrapper);
    }
}
